//-----------------------------------------------------------------------
// <copyright file="IDSSortDAO.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
	
using System;
using System.Data;
using MIMS.WRS.Entities;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MIMS.WRS.Data
{	
	public class ShoppingOrderDAO
	{
		public ShoppingOrderDAO()
		{
		}

		#region Common methods
		public virtual ShoppingOrder CreateShoppingOrderFromReader(IDataReader reader)
		{
			ShoppingOrder item = new ShoppingOrder();
			try
			{
				if (!reader.IsDBNull(reader.GetOrdinal("ShoppingOrderId"))) item.ShoppingOrderId = (int)reader["ShoppingOrderId"];
				if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (int)reader["UserId"];
				if (!reader.IsDBNull(reader.GetOrdinal("Qty20"))) item.Qty20 = (int)reader["Qty20"];
				if (!reader.IsDBNull(reader.GetOrdinal("Qty40"))) item.Qty40 = (int)reader["Qty40"];
				if (!reader.IsDBNull(reader.GetOrdinal("Qty40HC"))) item.Qty40HC = (int)reader["Qty40HC"];
				if (!reader.IsDBNull(reader.GetOrdinal("OrderType"))) item.OrderType = (bool)reader["OrderType"];
				if (!reader.IsDBNull(reader.GetOrdinal("OrderNo"))) item.OrderNo = (string)reader["OrderNo"];
				if (!reader.IsDBNull(reader.GetOrdinal("HalfPalletSpaces"))) item.HalfPalletSpaces = (int)reader["HalfPalletSpaces"];
				if (!reader.IsDBNull(reader.GetOrdinal("FilledVolume"))) item.FilledVolume = (decimal)reader["FilledVolume"];
				if (!reader.IsDBNull(reader.GetOrdinal("DestinationCountryId"))) item.DestinationCountryId = (int)reader["DestinationCountryId"];
				if (!reader.IsDBNull(reader.GetOrdinal("ConPointID"))) item.ConPointID = (int)reader["ConPointID"];
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateShoppingOrderFromReaderException, ex);
			}
			return item;
		}
		#endregion
        
		#region CreateShoppingOrder methods
            
		public virtual void CreateShoppingOrder(ShoppingOrder shoppingOrder)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingOrderInsert");
               
				dbCommandWrapper.AddInParameter("@UserId", DbType.Int32, ((shoppingOrder.UserId==0 || shoppingOrder.UserId ==Nulls.NullInteger)? DBNull.Value:(object)shoppingOrder.UserId));
				dbCommandWrapper.AddInParameter("@Qty20", DbType.Int32, shoppingOrder.Qty20);
				dbCommandWrapper.AddInParameter("@Qty40", DbType.Int32, shoppingOrder.Qty40);
				dbCommandWrapper.AddInParameter("@Qty40HC", DbType.Int32, shoppingOrder.Qty40HC);
				dbCommandWrapper.AddInParameter("@OrderType", DbType.Boolean, shoppingOrder.OrderType);
				dbCommandWrapper.AddInParameter("@OrderNo", DbType.String, shoppingOrder.OrderNo);
				dbCommandWrapper.AddInParameter("@HalfPalletSpaces", DbType.Int32,shoppingOrder.HalfPalletSpaces);
				dbCommandWrapper.AddInParameter("@FilledVolume", DbType.Decimal, shoppingOrder.FilledVolume);
				dbCommandWrapper.AddInParameter("@DestinationCountryId", DbType.Int32, shoppingOrder.DestinationCountryId);
				dbCommandWrapper.AddInParameter("@ConPointID", DbType.Int32, shoppingOrder.ConPointID);
				dbCommandWrapper.AddOutParameter("@ShoppingOrderId", DbType.Int32, 0);
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
				shoppingOrder.ShoppingOrderId = (int)dbCommandWrapper.GetParameterValue("@ShoppingOrderId");
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateShoppingOrderException, ex);
			}
		}

		#endregion

		#region UpdateShoppingOrder methods
        
		public virtual void UpdateShoppingOrder(ShoppingOrder shoppingOrder)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingOrderUpdate");            
                
				dbCommandWrapper.AddInParameter("@ShoppingOrderId", DbType.Int32, shoppingOrder.ShoppingOrderId);
				dbCommandWrapper.AddInParameter("@UserId", DbType.Int32, shoppingOrder.UserId);
				dbCommandWrapper.AddInParameter("@Qty20", DbType.Int32, shoppingOrder.Qty20);
				dbCommandWrapper.AddInParameter("@Qty40", DbType.Int32, shoppingOrder.Qty40);
				dbCommandWrapper.AddInParameter("@Qty40HC", DbType.Int32, shoppingOrder.Qty40HC);
				dbCommandWrapper.AddInParameter("@OrderType", DbType.Boolean, shoppingOrder.OrderType);
				dbCommandWrapper.AddInParameter("@OrderNo", DbType.String, shoppingOrder.OrderNo);
				dbCommandWrapper.AddInParameter("@HalfPalletSpaces", DbType.Int32, shoppingOrder.HalfPalletSpaces);
				dbCommandWrapper.AddInParameter("@FilledVolume", DbType.Decimal, shoppingOrder.FilledVolume);
				dbCommandWrapper.AddInParameter("@DestinationCountryId", DbType.Int32, shoppingOrder.DestinationCountryId);
				dbCommandWrapper.AddInParameter("@ConPointID", DbType.Int32, shoppingOrder.ConPointID);
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessUpdateShoppingOrderException, ex);
			}
		}
        
		#endregion

		#region DeleteShoppingOrder methods
		public virtual void DeleteShoppingOrder(int shoppingOrderId)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingOrderDelete");
                
				dbCommandWrapper.AddInParameter("@ShoppingOrderId", DbType.Int32, shoppingOrderId);
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessDeleteShoppingOrderException, ex);
			}
		}

		#endregion

		#region GetShoppingOrder methods
        
		public virtual ShoppingOrder GetShoppingOrder(int shoppingOrderId)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingOrderGet");
                
				dbCommandWrapper.AddInParameter("@ShoppingOrderId", DbType.Int32, shoppingOrderId);
                
				ShoppingOrder shoppingOrder =  null;
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					if (reader.Read())
					{
						shoppingOrder = CreateShoppingOrderFromReader(reader);
						reader.Close();
					}
				}
				return shoppingOrder;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetShoppingOrderException, ex);
			}
		}

		public virtual ShoppingOrderCollection GetShoppingOrderByUserId(int shoppingUserId)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingOrderGetByShoppingUserId");
                
				dbCommandWrapper.AddInParameter("@ShoppingUserId", DbType.Int32, shoppingUserId);
                
				ShoppingOrderCollection shoppingOrdCol = new ShoppingOrderCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while(reader.Read())
					{
						ShoppingOrder shoppingOrder = CreateShoppingOrderFromReader(reader);
						//There are more than one items returned due to allow to save shopping items by Cont.Point //reader.Close();
						shoppingOrdCol.Add(shoppingOrder);
					}
					reader.Close();
				}
				return shoppingOrdCol;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("DataAccessGetShoppingOrderByUserIdException", ex);
			}
		}
        
		public virtual bool IsExistedShoppingOrder(int shoppingUserId)
		{            
			bool isExisted = false;
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingOrderGetByUserId");
                
				dbCommandWrapper.AddInParameter("@ShoppingUserId", DbType.Int32, shoppingUserId);
                
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					if (reader.Read())
					{
						isExisted =true;
						reader.Close();
					}
				}
				return isExisted;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("DataAccessIsExistedShoppingOrderException", ex);
			}
			return isExisted;
		}

		#endregion

	}
}
